package com.example.androidsx;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class ShopListActivity extends AppCompatActivity {
      private SQLcj dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_list);
        dbHelper=new SQLcj(this);
        Button checkout=findViewById(R.id.btn_checkout);

        Toolbar toolbar = findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }

        TextView title = findViewById(R.id.toolbar_title);
        title.setText("商品分类");

        ImageButton btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(v -> onBackPressed());


        List<ShopInfo> shopList = loadProductDataFromDB();

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new ShopListAdapter(shopList, this));
        setupBottomButtons();

        checkout.setOnClickListener(v -> {
            Intent intent=new Intent(this,CheckoutActivity.class);
            startActivity(intent);
        });
    }
    private List<ShopInfo> loadProductDataFromDB() {
        List<ShopInfo> shopList = new ArrayList<>();
        Cursor cursor = dbHelper.getAllProducts();

        if (cursor != null && cursor.moveToFirst()) {
            do {
                String type = cursor.getString(cursor.getColumnIndexOrThrow(SQLcj.COLUMN_TYPE));
                double price = cursor.getDouble(cursor.getColumnIndexOrThrow(SQLcj.COLUMN_MONEY));
                int quantity = cursor.getInt(cursor.getColumnIndexOrThrow(SQLcj.COLUMN_NUMBER));


                shopList.add(new ShopInfo(
                        type,
                        "4.5分",
                        quantity + "件",
                        "价格: ¥" + price,
                        type.toLowerCase()
                ));
            } while (cursor.moveToNext());
            cursor.close();
        }

        return shopList;
    }


    private void setupBottomButtons() {
        Button btnHome = findViewById(R.id.btn_home);
        btnHome.setOnClickListener(v -> {

            RecyclerView recyclerView = findViewById(R.id.recycler_view);
            recyclerView.getAdapter().notifyDataSetChanged();
        });

        Button btnCart = findViewById(R.id.btn_cart);
        btnCart.setOnClickListener(v -> {
            PopupMenu popup = new PopupMenu(ShopListActivity.this, v);
            popup.getMenuInflater().inflate(R.menu.cart_menu, popup.getMenu());
            popup.show();
        });

        Button btnProfile = findViewById(R.id.btn_profile);
        btnProfile.setOnClickListener(v -> {
            startActivity(new Intent(this, AddressActivity.class));
        });

        Button btnCheckout = findViewById(R.id.btn_checkout);
        btnCheckout.setOnClickListener(v -> {
            startActivity(new Intent(this, CheckoutActivity.class));
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);


    }
}